package cn.jiyun.day4ceshi.controller;

import cn.jiyun.day4ceshi.pojo.Day3;
import cn.jiyun.day4ceshi.service.Uservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("user")
public class Ucontroller {
    @Autowired
    private Uservice uservice;

    @GetMapping("findAll")
    public String findAll(Model model){
        List<Day3> list=uservice.findAll();
        model.addAttribute("list",list);
        return "ulist";
    }

    @GetMapping("del")
    public String del(Integer id){
        uservice.del(id);
        return "redirect:findAll";
    }

    @GetMapping("tzadd")
    public String tzadd(){
        return "add";
    }

    @GetMapping("hx")
    public String hx(Integer id,Model model){
       Day3 day3= uservice.hx(id);
       model.addAttribute("user",day3);
        return "update";
    }

    @PostMapping("addOrupdate")
    public String addOrupdate(Day3 day3){
        if (day3.getId()==null){
            uservice.add(day3);
        }else {
            uservice.update(day3);
        }
        return "redirect:findAll";
    }
}
